set(CMAKE_PREFIX_PATH "E:/Code/Net/grpc/cmake/build/install")

# 查找依赖
find_package(Protobuf REQUIRED CONFIG)
find_package(gRPC REQUIRED CONFIG)

if (Protobuf_FOUND)
    message(STATUS "Protobuf include dir: ${Protobuf_INCLUDE_DIRS}")
    message(STATUS "Protobuf library: ${Protobuf_LIBRARY}")
    message(STATUS "Protobuf version: ${Protobuf_VERSION}")
else()
    message(FATAL_ERROR "Protobuf not found")
endif()

if (gRPC_FOUND)
    message(STATUS "gRPC include dir: ${gRPC_INCLUDE_DIRS}")
    message(STATUS "gRPC library: ${gRPC_LIBRARY}")
    message(STATUS "gRPC version: ${gRPC_VERSION}")
else()
    message(FATAL_ERROR "gRPC not found")
endif()    

file(GLOB PROTO_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")

add_library(protocol ${PROTO_FILES})

target_link_libraries(protocol
    PUBLIC
        protobuf::libprotobuf
        gRPC::grpc
        gRPC::grpc++
)

target_include_directories(protocol PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

# 生成代码
get_target_property(grpc_cpp_plugin_location gRPC::grpc_cpp_plugin LOCATION)
protobuf_generate(TARGET protocol LANGUAGE cpp)
protobuf_generate(TARGET protocol LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${grpc_cpp_plugin_location}")
